﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using LevelEditor.App;

namespace LevelEditor
{
    public partial class LoadSpriteSheet : Form
    {
        private Image loadedSpriteSheet;
        public Image LoadedSpriteSheet
        {
            get { return loadedSpriteSheet; }
        }

        private DialogResult result;

        public LoadSpriteSheet()
        {
            InitializeComponent();

            OpenFileDialog imageLoader = new OpenFileDialog();
            imageLoader.Filter = "PNG Files|*.png";
            
            if (imageLoader.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileStream fs = new FileStream(imageLoader.FileName, FileMode.Open);
                loadedSpriteSheet = Bitmap.FromStream(fs);
                fs.Close();
                FillImageDisplayer(loadedSpriteSheet);
            }
        }

        public LoadSpriteSheet(Image img)
        {
            InitializeComponent();

            FillImageDisplayer(img);
        }

        private void FillImageDisplayer(Image img)
        {
            if (img == null)
                ErrorController.ShowOkError(ErrorController.LevelEditorError.APP_IMGSHEET_LOAD);

            PictureBox picBox = new PictureBox();
            picBox.Image = img;
            picBox.SizeMode = PictureBoxSizeMode.StretchImage;

            picBox.Width = spriteSheetDisplayer.Width;

            picBox.Height = spriteSheetDisplayer.Height;

            spriteSheetDisplayer.Controls.Add(picBox);
        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            this.result = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            result = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        public new DialogResult ShowDialog()
        {
            base.ShowDialog();

            return result;
        }
    }
}
